package com.web.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;

/**
 * Created by reeves on 2015/7/5.
 * <p>
 * 注：PageFirstType 作为一个Model，但是数据库中没有与之对应的表或视图，属于抽象的表，因为这个Model里面数据操作是使用Db来做的
 */
public class PageFirstType extends Model<PageFirstType> {
    public static final PageFirstType pageDao = new PageFirstType();

    /**
     * 获取一级分类分页数据
     * 一级分类界面：1. 信息都是今天上传的；2. 一级分类滑图信息显示在最上面（已审核）；3. 可以审核信息，查看信息内容；4. 未审核的信息显示在最上面；
     *
     * @param pageNumber
     * @param pageSize
     * @param firstType
     * @return
     */
    public Page<Record> getFirstPage(int pageNumber, int pageSize, int firstType) {

        /**
         * 在这里区别firstType是否大于0，大于零是除了推荐外的其他一级分类，0作为推荐知识在此项目中的一个约定而已，方便接口统一
         */
        if (firstType > 0) {
            return Db.paginate(pageNumber, pageSize, "SELECT page_edit.id,pageDate,pageType,imageSrc,firstTitle,editTypeId,thirdTypeName,pageFrom,recomPos,isReady,name,sourceAddress", "FROM page_edit,page_titleimage,type_info,com_userinfo WHERE page_edit.editorId=com_userinfo.id AND page_edit.titleImage=page_titleimage.imageId AND page_edit.typeId=type_info.id AND firstType=? ORDER BY pageDate DESC,uploadDate DESC,isReady ASC,typeId ASC", firstType);
        } else {
            // 1. 这里的推荐界面是选过位置的推荐信息；2. 这里的信息都是审核过的；3.这些信息都是当天上传的信息
            return Db.paginate(pageNumber, pageSize, "SELECT page_edit.id,pageDate,imageSrc,imageSrc2,firstTitle,secondTitle,thirdTypeName,pageFrom,recomPos,name,sourceAddress", "FROM page_edit,page_titleimage,type_info,com_userinfo WHERE page_edit.editorId=com_userinfo.id AND page_edit.titleImage=page_titleimage.imageId AND page_edit.typeId=type_info.id AND pageType=2 AND isTodayRecom=1 ORDER BY recomPos ASC,subjectOrderIndex ASC");
        }
    }

    /**
     * 从page_edit表中获取明日划图推荐
     */
    public List<Record> getSlidePage(int firstType){
        if(firstType==0){
            return Db.find("SELECT page_edit.id,pageDate,imageSrc,imageSrc2,firstTitle,thirdTypeName,pageFrom,recomPos,name,sourceAddress from page_edit,page_titleimage,type_info,com_userinfo WHERE page_edit.editorId=com_userinfo.id AND page_edit.titleImage=page_titleimage.imageId AND page_edit.typeId=type_info.id AND isTodayRecom = 1 AND pageType = 1");
        }
        else{
            return Db.find("SELECT page_edit.id,pageDate,imageSrc,imageSrc2,firstTitle,secondTitle,thirdTypeName,pageFrom,recomPos,name,sourceAddress from page_edit,page_titleimage,type_info,com_userinfo WHERE page_edit.editorId=com_userinfo.id AND page_edit.titleImage=page_titleimage.imageId AND page_edit.typeId=type_info.id AND pageType=2 AND recomPos=1 ORDER BY pageDate DESC LIMIT 0,5");
        }
    }

    /**
     * 只从age_edit表中获取明日推荐划图的图片地址和id
     */
    public List<Record> getRecomSlide(){
        return Db.find("SELECT page_edit.id,imageSrc2 from page_edit,page_titleimage WHERE page_edit.titleImage=page_titleimage.imageId  AND pageType=2 AND recomPos=1 ORDER BY pageDate DESC LIMIT 0,5");
    }

    /**
     * 获取一级分类分页总数
     *
     * @param pageSize
     * @param firstType
     * @return
     */
    public int getPageNumberFirst(int pageSize, int firstType) {
        String sqlStr = "";
        Long countLong;

        if(firstType>0) {
            sqlStr = "SELECT COUNT(*) FROM page_edit,type_info WHERE page_edit.typeId=type_info.id AND firstType=?";
            countLong = Db.queryLong(sqlStr, firstType);
        }else {
            sqlStr = "SELECT COUNT(*) FROM page_edit WHERE isReady=1 AND recomPos>0";
            countLong = Db.queryLong(sqlStr);
        }

        int count = countLong.intValue();
        return count % pageSize == 0 ? (count / pageSize) : (count / pageSize + 1);
    }


}
